import React, { Component } from 'react';
import MyNavBar from '../../components/MyNavBar'
import { WingBlank, WhiteSpace, Flex, Toast } from 'antd-mobile'
import styles from './index.module.scss'
import { setToken } from 'utils/token';
class Login extends Component {
    constructor() {
        super()
        this.state = {
            // 用户名
            username: "test2",
            // 密码
            password: "test2",
        }
    }
    changeValue = (e) => {
        this.setState({
            // 属性表达式
            [e.target.name]: e.target.value
        })
    }
    Login = async () => {
        const res = await this.axios.post("user/login", this.state)
        Toast.info(res.data.description)
        console.log(res, "=========")
        if (res.data.status === 200) {
            // localStorage.setItem("hkzf_token",res.data.body.token)
            setToken(res.data.body.token)
            if (this.props.location.state) {
                this.props.history.replace(this.props.location.state.toPath)
            } else {
                this.props.history.goBack()
            }
        }
    }
    render() {
        const { username, password } = this.state
        return (
            <div className={styles.root}>
                <MyNavBar title="账号登录" />
                <WhiteSpace size="lg" />
                <WingBlank size="lg">
                    <div className={styles.fromItem}>
                        <input type="text" placeholder="请输入用户名" value={username} name="username" className={styles.input} onChange={this.changeValue} />
                    </div>
                    <div className={styles.fromItem}>
                        <input type="password" placeholder="请输入密码" value={password} className={styles.input} name="password" onChange={this.changeValue} />
                    </div>
                    <WhiteSpace size="lg" />
                    <WhiteSpace size="lg" />
                    <div className={styles.fromSubmit}>
                        <div onClick={this.Login} className={styles.submit}>登录</div>
                    </div>
                    <Flex className={styles.backHome}>
                        <Flex.Item onClick={()=>{this.props.history.replace("/register")}}>
                            <a>还没有账号 , 去注册~</a>
                        </Flex.Item>
                    </Flex>
                </WingBlank>
            </div>
        );
    }
}

export default Login;